 var appPath = getAppPath();
//初始化加载数据
$(function () {
	var uGrpBy = $("#queryGrpBy").combobox('getValue');
	var uPage = $("#queryPage").combobox('getValue');
	var obj = {'finGrpBy':uGrpBy,"findPage":uPage};
	
	var dGrpBy = $("#queryDocGrpBy").combobox('getValue');
	var dPage = $("#queryDocPage").combobox('getValue');
	var data = {"grpBy":dGrpBy,"rows":dPage};
	initData(obj);//加载用户图
	initDocData(data);//加载文档图
	initSumInfo();//加载总数
});

function initSumInfo(){
	var url = appPath + "/itbsys/getTotalStatisInfo.action";
	$.get(url,function(ret){
		if(ret){
			var info = ret[0];
			$("#sumU").html(info.sumU);
			$("#tnum").html(info.tnum);
			$("#sumD").html(info.sumD);
			$("#sumTgCnt").html(info.sumTgCnt);
			$("#sumTgPgCnt").html(info.sumTgPgCnt);
			$("#sumPtCnt").html(info.sumPtCnt);
			$("#sumPtPgCnt").html(info.sumPtPgCnt);
		}
	});
}


function initData(obj){
	$.loading(true);
	var url = appPath+"/itbsys/getSysUserChartInfo.action";
	var res = [];
	$.get(url,obj,function(list){
		var newdata=list;
        function sortDay(a,b){  
           return a.day-b.day  
        }  
        //利用js中的sort方法  
        newdata.sort(sortDay);  
		for (var i in list) {
			var item = [];
			for (var j in list[i]) item.push(list[i][j]);
			res.push(item)
		}
		var st = obj.finGrpBy;
		initUserChart(st,res);
		if(list){
			$.loading(false);
		}
	});
}

function initDocData(data){
	$.loading(true);
	var res = [];
	var url1 = appPath+"/itbsys/getNumUserDocInfo.action";
	$.get(url1,data,function(ret){
		var newdata = ret;
		function sortDay(a,b){  
	       return a.day-b.day  
	    }  
		newdata.sort(sortDay);
		for(var i in newdata){
			var item = [];
			for (var j in newdata[i]) item.push(newdata[i][j]);
			res.push(item)
		}
		var st = data.grpBy;
		initDocChart(st,res);
		if(ret){
			$.loading(false);
		}
	});
}


function initUserChart(st,data){
	var myChart = echarts.init(document.getElementById('main'));
	if(st ==1){
		var dateList = data.map(function (item) {
			return item[0];
		});
	}else if(st ==2){
		var dateList = data.map(function (item) {
			return item[3];
		});
	}else if(st ==3){
		var dateList = data.map(function (item) {
			return item[1];
		});
	}
	var valueList = data.map(function (item) {
		return item[2];
	});
	option = {
		    visualMap: [{
		        show: false,
		        type: 'continuous',
		        seriesIndex: 0,
		        min: 0,
		        max: 400
		    }],
		    title: [{
		        left: 'center',
		        text: '注册用户数量增长折线图'
		    }],
		    tooltip: {
		        trigger: 'axis'
		    },
		    xAxis: [{
		        data: dateList
		    }],
		    yAxis: [{
		        splitLine: {show: false}
		    }],
		    grid: [{
		        bottom: '10%'
		    }],
		    series: [{
		        type: 'line',
		        showSymbol: false,
		        data: valueList
		    }]
		};
	 myChart.setOption(option);
}

function doQuery(){
	var obj = {};
	var grpBy = $("#queryGrpBy").combobox('getValue');
	var page = $("#queryPage").combobox('getValue');
	if(Number(grpBy)>0){
		obj.finGrpBy = grpBy;
	}
	if(Number(page)>0){
		obj.findPage = page;
	}
	var dtS = $("#queryDateStart").datebox('getValue');
	var dtE = $("#queryDateEnd").datebox('getValue');
	if(dtS){
		var findDtS = chkInputDate(dtS);
		if(findDtS && findDtS.length>0){
			obj.findSdat = findDtS;
		}
	}
	if(dtE){
		var findDtE = chkInputDate(dtE);
		if(findDtE && findDtE.length>0){
			obj.findEdat = findDtE;
		}
	}
	initData(obj);
	initSumInfo();
}
function doQuery2(){
	var obj = {};
	var grpBy = $("#queryDocGrpBy").combobox('getValue');
	var page = $("#queryDocPage").combobox('getValue');
	if(Number(grpBy)>0){
		obj.grpBy = grpBy;
	}
	if(Number(page)>0){
		obj.rows = page;
	}
	var dtS = $("#queryDocDateStart").datebox('getValue');
	var dtE = $("#queryDocDateEnd").datebox('getValue');
	if(dtS){
		var findDtS = chkInputDate(dtS);
		if(findDtS && findDtS.length>0){
			obj.findDocDts = findDtS;
		}
	}
	if(dtE){
		var findDtE = chkInputDate(dtE);
		if(findDtE && findDtE.length>0){
			obj.findDocDte = findDtE;
		}
	}
	initDocData(obj);
	initSumInfo();
}

function dtFmt(dt){
		var y = dt.getFullYear();
	    var m = dt.getMonth()+1;
	    var d = dt.getDate();
	    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);//格式化mm-dd
}

function dtParser(s){
	    if (!s) return new Date();
	    var ss = (s.split('-'));
	    var y = parseInt(ss[0],10);
	    var m = parseInt(ss[1],10);
	    var d = parseInt(ss[2],10);
	    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
	        return new Date(y,m-1,d);
	    } else {
	        return new Date();
	    }
}
//判断日期格式[YYYY-MM-DD]
 function chkInputDate(dt){
 	 var strDate=dt+'';
     var ss = (strDate.split('-'));
     var y = parseInt(ss[0],10);
     var m = parseInt(ss[1],10);
     var d = parseInt(ss[2],10);
     if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
         var chkDt = new Date(y,m-1,d);
 		if((chkDt.getFullYear()==y) &&((chkDt.getMonth()+1)==m) &&(chkDt.getDate()==d)) {
         	return dtFmt(chkDt);
         }
     }
     return '';
 }
 
 function initDocChart(st,data){
		if(st ==1){
			var dateList = data.map(function (item) {
				return item[0];
			});
		}else if(st ==2){
			var dateList = data.map(function (item) {
				return item[8];
			});
		}else if(st ==3){
			var dateList = data.map(function (item) {
				return item[2];
			});
		}
		var ppCnt = data.map(function (item) {
			return item[4];
		});
		var ppPgCnt = data.map(function (item) {
			return item[5];
		});
		var tgCnt = data.map(function (item) {
			return item[6];
		});
		var tgPgCnt = data.map(function (item) {
			return item[7];
		});
		var valueList = data.map(function (item) {
			return item[2];
		});
	 var myChart = echarts.init(document.getElementById('mdoc'));
	 option = {
			  	title: [{
			        left: 'right',
			        text: '文档数量增长图'
			    }],
			    tooltip: {
			        trigger: 'axis'
			    },
			    legend: {
			    	data: ['设计模板','PPt模板','设计文档数','PPt文档数']
			    },
			    grid: {
			        left: '3%',
			        right: '4%',
			        bottom: '3%',
			        containLabel: true
			    },
			    toolbox: {
			        feature: {
			            saveAsImage: {}
			        }
			    },
			    xAxis: {
			        type: 'category',
			        boundaryGap: false,
			        data: dateList
			    },
			    yAxis: {
			        type: 'value'
			    },
			    series: [{
			            name:'设计模板',
			            type:'line',
			            stack: '总量1',
			            data:tgCnt
			        },
			        {
			            name:'PPt模板',
			            type:'line',
			            stack: '总量2',
			            data:ppCnt
			        },
			        {
			            name:'设计文档数',
			            type:'line',
			            stack: '总量3',
			            data:tgPgCnt
			        },
			        {
			            name:'PPt文档数',
			            type:'line',
			            stack: '总量4',
			            data:ppPgCnt
			        }]
			};
	 myChart.setOption(option);
 }
